import argparse

class Step:
    """基本步骤类"""
    def __init__(self, step_id, description):
        self.step_id = step_id
        self.description = description

    def execute(self):
        pass

    def get_info(self):
        return f"Step ID: {self.step_id}, Description: {self.description}"


class Step1(Step):
    def __init__(self):
        super().__init__(1, "Initialize resources.")

    def execute(self):
        print("Executing:", self.get_info())


class Step2(Step):
    def __init__(self):
        super().__init__(2, "Load data.")

    def execute(self):
        print("Executing:", self.get_info())


class Step3(Step):
    def __init__(self):
        super().__init__(3, "Process data.")

    def execute(self):
        print("Executing:", self.get_info())


class Step4(Step):
    def __init__(self):
        super().__init__(4, "Validate data.")

    def execute(self):
        print("Executing:", self.get_info())


class Step5(Step):
    def __init__(self):
        super().__init__(5, "Save results.")

    def execute(self):
        print("Executing:", self.get_info())


class Step6(Step):
    def __init__(self):
        super().__init__(6, "Generate reports.")

    def execute(self):
        print("Executing:", self.get_info())


class Step7(Step):
    def __init__(self):
        super().__init__(7, "Send notifications.")

    def execute(self):
        print("Executing:", self.get_info())


class Step8(Step):
    def __init__(self):
        super().__init__(8, "Clean up resources.")

    def execute(self):
        print("Executing:", self.get_info())


class Step9(Step):
    def __init__(self):
        super().__init__(9, "Log activities.")

    def execute(self):
        print("Executing:", self.get_info())


class Step10(Step):
    def __init__(self):
        super().__init__(10, "Finalize tasks.")

    def execute(self):
        print("Executing:", self.get_info())


class Task:
    def __init__(self):
        self.steps = {
            1: Step1(),
            2: Step2(),
            3: Step3(),
            4: Step4(),
            # 假设步骤 5 缺失
            6: Step6(),
            7: Step7(),
            8: Step8(),
            # 假设步骤 9 缺失
            10: Step10(),
        }  # 创建步骤类的实例，存放在字典中

    def execute_steps(self, step_ids=None, start=None, end=None):
        """
        执行子步骤。

        :param step_ids: 要执行的步骤 ID 列表
        :param start: 起始步骤 ID
        :param end: 结束步骤 ID
        """
        if step_ids is None:
            # 如果没有提供步骤，则询问用户
            print("Available steps:")
            for step_id, step in self.steps.items():
                print(step.get_info())
            
            try:
                step_ids_input = input("Enter the step IDs to execute (comma-separated) or specify a range (start-end): ")
                if ',' in step_ids_input:
                    step_ids = [int(step_id.strip()) for step_id in step_ids_input.split(',')]
                elif '-' in step_ids_input:
                    start, end = map(int, step_ids_input.split('-'))
                else:
                    print("Invalid input format.")
                    return
            except ValueError:
                print("Invalid input. Please enter valid step IDs.")
                return

        if start is not None and end is not None:
            # 如果同时提供了起始和结束范围
            step_ids = [i for i in range(start, end + 1)]

        for step_id in step_ids:
            if step_id in self.steps:
                self.execute_step(step_id)  # 执行步骤
            else:
                print(f"Step {step_id} is not available, skipping.")

    def execute_step(self, step_number):
        """执行单个步骤"""
        print(f"Executing: Step {step_number}")
        self.steps[step_number].execute()  # 调用对应的步骤方法


def main():    
    # 使用 argparse 模块从命令行获取参数
    parser = argparse.ArgumentParser(description='Execute steps in a task.')
    parser.add_argument('--steps', type=str, help='Comma-separated list of step IDs to execute')
    parser.add_argument('--start', type=int, help='Starting step ID')
    parser.add_argument('--end', type=int, help='Ending step ID')

    args = parser.parse_args()

    task = Task()
    step_ids = None
    if args.steps:
        step_ids = [int(step.strip()) for step in args.steps.split(',')]
    
    task.execute_steps(step_ids, args.start, args.end)


# 示例用法
if __name__ == "__main__":
    print('执行子步骤')
    # main()
